﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;

namespace Kooboo.Commerce.Contexts.Accounts
{
    public static class EditorBehavior
    {
        #region Administrator
        public static IQueryable<IAdministrator> QueryAdministrators(this IEditor actor)
        {
            return ContextContainer.Current.Resolve<IAccountRepository>().Administrators;
        }
       
        public static void Post(this IEditor actor,IAdministrator administrator)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IAccountRepository>();
                    repository.Post(administrator);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public static void Delete(this IEditor actor,IAdministrator administrator)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IAccountRepository>();
                    repository.Delete(administrator);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
        #endregion
    }
}
